﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Servers
{
    public class SettingServers
    {
	    private readonly IRepository<user> usersRepository;

        public SettingServers()
        {
            usersRepository = new DapperRepository<user>();

        }

        public List<user> GetData()
        {
            var datas = usersRepository.GetAllEnumerable();

					  			return datas.ToList();
        }

        public user GetModelById(string id)
        {
            return usersRepository.GetById(id);
        }

		
        internal void Add(user model)
        {
            usersRepository.Insert(model);
        }

        internal void Update(user model)
        {
            usersRepository.Update(model);
        }

		internal void Del(string id)
        {
            usersRepository.DeleteByIds(id);
        }

				      
        internal List<SelectListItem> GetSEX()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = usersRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 148");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal List<SelectListItem> GetCALL_SOUND()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = usersRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 148");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetNOT_LOGIN()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = usersRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal bool CheckPwd(user model)
        {
            var sql = "select * from users where USER_PWD=@USER_PWD";
            return usersRepository.SqlWithParamsBool(sql, new{USER_PWD= model.OLD_USER_PWD});
        }

        internal void UpdatePwd(user model)
        {
            var sql = "update users set USER_PWD=@USER_PWD,LAST_PASS_TIME=@LAST_PASS_TIME where USER_ID=@USER_ID";
            usersRepository.SqlWithParams(sql, new { model.USER_PWD, LAST_PASS_TIME=DateTime.Now, model.USER_ID});
        }
    }
}